//
//  SearchPBoard.m
//  iHeima
//
//  Created by WL-DZ-PGDN-007 on 14-6-29.
//  Copyright (c) 2014年 leon. All rights reserved.
//

#import "SearchPBoard.h"

@interface SearchPBoard ()<UIWebViewDelegate>{
    UIWebView* _webview;
    
    NSString* _action;
    NSString* _id;
    
    NSDictionary* _actionAndID;
   // BOOL isShoudToNew;
    
    NSString* _afterAddingUrl;
    
}

@end

@implementation SearchPBoard

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"查找感兴趣的人";
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    [self showNaviBar];
    [self loadContent];
   
    
    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:singleTap];
    singleTap.delegate = self;
    singleTap.cancelsTouchesInView = NO;
    
}
-(void)loadContent{
    
    
    _webview = [[UIWebView alloc]init];
    _webview.frame = CGRectMake(0, 0,self.viewWidth, self.viewHeight-(IOS7_OR_LATER?64:44));
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/app/searchshow",kBaseUrl]]]];
    
    _webview.delegate = self;
    
    [self.view addSubview:_webview];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    
    
    
    //resignFirstResponder
    [_webview  stringByEvaluatingJavaScriptFromString:@"document.activeElement.blur()"];
    
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    
    
    NSString* nextUrl = [request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"将要出现的URL：%@",nextUrl);
 

    
    NSArray* arr = [nextUrl componentsSeparatedByString:@"/"];
    NSLog(@"当前数组内元素个数:%d",arr.count);
    if (arr.count == 7) {
        
        if ([[arr objectAtIndex:4] isEqualToString:@"myhome"] ) {
            NSLog(@"该换页面了");
            
            [webView stopLoading];
            [self toNewUrlByOriginUrl:nextUrl];
            
            _afterAddingUrl = nextUrl;
            
           
        }
    }

    
    
    
    
    
    
    if([nextUrl componentsSeparatedByString:@"#"].count == 5){
       
        [webView stopLoading];
        
        NSArray* ac = [nextUrl componentsSeparatedByString:@"#"];
        NSArray* arr = [[ac objectAtIndex:2] componentsSeparatedByString:@"&"];
        NSLog(@"----%@",[arr objectAtIndex:0]);
        NSLog(@"=====%@",[arr objectAtIndex:1]);
        
        //action
        _action =[[[arr objectAtIndex:0] componentsSeparatedByString:@"="] objectAtIndex:1];
        NSLog(@"动作：%@",[[[arr objectAtIndex:0] componentsSeparatedByString:@"="] objectAtIndex:1]);
        _id = [[[arr objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1];
        NSLog(@"fid:%@",[[[arr objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1]);
        
        if ([_action isEqualToString:@"addfriend"] && _id) {
            //发送 添加好友请求
            
            [self addFriendByFid:_id];
            
            
        }
        
        
        
       
        
    
        
        
        
    }
    
    
    return YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    
    
    NSLog(@"结束时的URL：%@",webView.request.URL.absoluteString);
}

- (void)toNewUrlByOriginUrl:(NSString*)originUrl{
    
    
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/myid/%@",originUrl,kUserInfo[@"uid"]]]]];
    
}





- (void)addFriendByFid:(NSString*)fuid{
    

    
    
    [self POST:[NSString stringWithFormat:@"%@/apiuser/addfriend",kBaseUrl]].PARAM(@"fuid",fuid).PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"message",@"addFriend");
    
    
   //[self POST:[NSString stringWithFormat:@"%@/apiuser/addfriend",kBaseUrl]].PARAM(@"uid",fuid).PARAM(@"fuid",kUserInfo[@"uid"]).PARAM(@"message",@"addFriend");
    
    
    
    
    
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"添加好友的JSON返回-----------%@",json);
        NSLog(@"info:%@",json[@"info"]) ;
        
        UIAlertView* av = [[UIAlertView alloc]initWithTitle:@"添加好友成功" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil, nil];
        [av show];
        
        if (_afterAddingUrl) {
            [self toNewUrlByOriginUrl:_afterAddingUrl];
        }
        
        
        
        
        
    }
}







- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
